package com.ybear.ybhotrepair.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 日志类
 */
public class LogUtil {
    private static CallLog mCallLog;
    public interface CallLog { void call(String tag, String message, String type); }

    private static final String TAG = "YBHotRepairTAG";
    private static final String TYPE_LOG_V = "verbose";
    private static final String TYPE_LOG_D = "debug";
    private static final String TYPE_LOG_I = "info";
    private static final String TYPE_LOG_W = "warn";
    private static final String TYPE_LOG_E = "error";

    private static boolean isDebug = false;
    private static boolean isWrite = false;

    /**
     * 根据类型打印日志
     * @param msg   msg
     * @param type  type
     */
    private static int printLog(String tag, String msg, String type) {
        if( isWrite ) saveLog(tag, msg, type);
        if( isDebug ) {
            tag = tag == null ? TAG : tag;
            msg = msg == null ? "" : msg;
            if ( type.equals(TYPE_LOG_D) ) return Log.d(tag, msg);
            if ( type.equals(TYPE_LOG_I) ) return Log.i(tag, msg);
            if ( type.equals(TYPE_LOG_W) ) return Log.w(tag, msg);
            if ( type.equals(TYPE_LOG_E) ) return Log.e(tag, msg);
            return Log.v(TAG, msg);
        }
        return -1;
    }

    /**
     * 保存日志
     * @param msg   日志内容
     * @param type  日志类型
     */
    private static void saveLog(String tag, String msg, String type) {
        if( mCallLog != null ) mCallLog.call(tag, msg, type);
    }

    public static boolean isDebug() {
        return isDebug;
    }

    /**
     * 允许启用输出日志功能
     * @param enable    正式环境（release）下日志不会输出（false）。
     *                  除非你在manifest的application中配置：android:debuggable="true"
     */
    public static void setDebugEnable(@NonNull Context mContext, boolean enable) {
        ApplicationInfo info = mContext.getApplicationInfo();
        //传入true时会验证当前环境是否也处于调试模式
        isDebug = enable && (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    }

    /**
     * 是否允许启用日志保存功能
     * @param enable    是否启用
     */
    public static void setSaveLogEnable(boolean enable) {
        isWrite = enable;
    }

    /**
     * 启用日志后会回调日志接口，保存操作交付给开发者自行保存
     * @param call  回调
     */
    public static void setOnSaveListener(CallLog call) {
        mCallLog = call;
    }

    public static int v(@Nullable String tag, @Nullable String msg) {
        return printLog(tag, msg, TYPE_LOG_V);
    }
    public static int d(@Nullable String tag, @Nullable String msg) {
        return printLog(tag, msg, TYPE_LOG_D);
    }
    public static int i(@Nullable String tag, @Nullable String msg) {
        return printLog(tag, msg, TYPE_LOG_I);
    }
    public static int w(@Nullable String tag, @Nullable String msg) {
        return printLog(tag, msg, TYPE_LOG_W);
    }
    public static int e(@Nullable String tag, @Nullable String msg) {
        return printLog(tag, msg, TYPE_LOG_E);
    }

    public static int v(String msg) { return v(null, msg); }
    public static int d(String msg) { return d(null, msg); }
    public static int i(String msg) { return i(null, msg); }
    public static int w(String msg) { return w(null, msg); }
    public static int e(String msg) { return e(null, msg); }
}